package com.jxc.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
import org.springframework.transaction.annotation.Transactional;
import com.jxc.dao.TbMeunDAO;
import com.jxc.entity.*;

/**
 * Data access object (DAO) for domain model class TbMenu.
 * 
 * @see com.jxc.dao.impl.TbMenu
 * @author MyEclipse Persistence Tools
 */
@Transactional
public class TbMenuDAOImpl implements TbMeunDAO{
	@Resource private SessionFactory sessionFactory;
	private static final Log log = LogFactory.getLog(TbMenuDAOImpl.class);

	/* (non-Javadoc)
	 * @see com.jxc.dao.impl.TbMeunDAO#save(com.jxc.entity.TbMenu)
	 */
	public void save(TbMenu transientInstance) {
		log.debug("saving TbMenu instance");
		try {
			
			sessionFactory.getCurrentSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.jxc.dao.impl.TbMeunDAO#delete(com.jxc.entity.TbMenu)
	 */
	public void delete(TbMenu persistentInstance) {
		log.debug("deleting TbMenu instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.jxc.dao.impl.TbMeunDAO#findById(java.lang.Integer)
	 */
	public TbMenu findById(java.lang.Integer id) {
		log.debug("getting TbMenu instance with id: " + id);
		try {
			TbMenu instance = (TbMenu) sessionFactory.getCurrentSession().get("com.jxc.dao.impl.TbMenu", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.jxc.dao.impl.TbMeunDAO#findByExample(com.jxc.entity.TbMenu)
	 */
	public List findByExample(TbMenu instance) {
		log.debug("finding TbMenu instance by example");
		try {
			List results =sessionFactory.getCurrentSession().createCriteria(
					"com.jxc.dao.impl.TbMenu").add(Example.create(instance))
					.list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.jxc.dao.impl.TbMeunDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
	public List findByProperty(String propertyName,int value) {
		log.debug("finding TbMenu instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from TbMenu as model where model.parentId="+value+"";
			Query queryObject = sessionFactory.getCurrentSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	
	/* (non-Javadoc)
	 * @see com.jxc.dao.impl.TbMeunDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
	public List findByParentID(int parentID) {
		try {
			String queryString = "from TbMenu as model where model.parentId="+parentID;
			Query queryObject = sessionFactory.getCurrentSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.jxc.dao.impl.TbMeunDAO#findAll()
	 */
	public List findAll() {
		log.debug("finding all TbMenu instances");
		try {
			String queryString = "from TbMenu";
			Query queryObject =sessionFactory.getCurrentSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.jxc.dao.impl.TbMeunDAO#merge(com.jxc.entity.TbMenu)
	 */
	public TbMenu merge(TbMenu detachedInstance) {
		log.debug("merging TbMenu instance");
		try {
			TbMenu result = (TbMenu) sessionFactory.getCurrentSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.jxc.dao.impl.TbMeunDAO#attachDirty(com.jxc.entity.TbMenu)
	 */
	public void attachDirty(TbMenu instance) {
		log.debug("attaching dirty TbMenu instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.jxc.dao.impl.TbMeunDAO#attachClean(com.jxc.entity.TbMenu)
	 */
	public void attachClean(TbMenu instance) {
		log.debug("attaching clean TbMenu instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}